package com.jwt.controller;

import com.jwt.pojo.Jwt;
import com.jwt.service.impl.JwtServiceImpl;
import com.jwt.utils.JwtTokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class JwtController {

    @Autowired
    private JwtServiceImpl jwtService;

    @Autowired
    private JwtTokenUtil jwtUtil;


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public void register(@RequestBody Jwt jwt) throws Exception {
        jwtService.login(jwt);
    }

    @RequestMapping(value = "/resolveToken", method = RequestMethod.POST)
    public void resolveToken(@RequestParam("token") String token) throws Exception {
        String resolveToken = jwtService.resolveToken(token);
    }


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public void TestToken() {
        String token = jwtUtil.generateToken("laoxu", "user");
        System.out.println("token: "+token);

        Long expiration = jwtUtil.getExpiration();
        System.out.println("expiration: "+expiration);

        String username = jwtUtil.getUsernameFromToken(token);
        System.out.println("username: "+username);

        Boolean tokenExpired = jwtUtil.isTokenExpired(token);
        System.out.println("bool: "+tokenExpired);

        String refreshToken = jwtUtil.refreshToken(token);
        System.out.println("refreshToken: "+refreshToken);


        String rolesFromToken = jwtUtil.getRolesFromToken(token);
        System.out.println("rolesFromToken: "+rolesFromToken);


    }

}
